<?php
/**
 * 海外仓-仓库运营商
 * (model)
 */
namespace App\Models\OverSeasBa;

use Illuminate\Database\Eloquent\Model;
class WarehouseGroup extends Model
{
    protected $table = 'os_b_warehouse_group';

    public $timestamps = false;

    protected $guarded = [];

    public static function options()
    {
        return self::query()->pluck('name','id');
    }

    public static function getNameById($id){
        return self::query()->where('id',$id)->value('name');
    }

    public static function getIdByName($name){
        return self::query()->where('name',$name)->value('id');
    }

    public static function getNameByIds($ids){
        return self::query()->whereIn('id',$ids)->pluck('name','id');
    }

    protected static function boot()
    {
        parent::boot();

        static::deleting(function ($model){
            $count = Warehouse::query()->where('group_id',$model->id)->count();
            if($count > 0){
                throw new \Exception('使用中勿删除');
            }
        });
    }
}
